﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.core {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.external.*;
    import reprise.css.*;
    import reprise.events.*;
    import flash.display.*;
    import reprise.utils.*;
    import flash.utils.*;
    import flash.external.*;

    public class Application extends Sprite {

        protected var m_appContext:ApplicationContext;
        protected var m_rootElement:DocumentView;
        protected var m_currentView:UIObject;
        protected var m_lastView:UIObject;
        protected var m_css:CSS;

        protected static const CSS_URL:String = "flash.css";

        public function Application(){
            if (stage){
                initialize();
            } else {
                addEventListener(Event.ADDED_TO_STAGE, self_addedToStage, false, 0, true);
            };
        }
        protected function showView(_arg1:Class, _arg2:Boolean):UIObject{
            if (m_currentView){
                m_lastView = m_currentView;
                m_currentView = null;
                m_lastView.addEventListener(DisplayEvent.HIDE_COMPLETE, lastView_hide);
                m_lastView.hide();
            };
            m_currentView = UIObject(m_rootElement.addChild(UIObject(new (_arg1))));
            if (((!(m_lastView)) && (!(_arg2)))){
                m_currentView.show();
            } else {
                m_currentView.setVisibility(false);
            };
            return (m_currentView);
        }
        protected function resource_complete(_arg1:Event):void{
            m_appContext.coreResourceLoader.removeEventListener(Event.COMPLETE, resource_complete);
            initApplication();
        }
        protected function startApplication():void{
        }
        protected function addResource(_arg1:IResource):IResource{
            m_appContext.coreResourceLoader.addResource(_arg1);
            return (_arg1);
        }
        public function basePath():String{
            return (PathUtil.stringByDeletingLastPathComponent(stage.loaderInfo.url));
        }
        protected function initialize():void{
            var _local1:String = getQualifiedClassName(this).split("::").pop();
            zz_init(stage, _local1);
            m_appContext = new ApplicationContext(this, this.loaderInfo);
            ApplicationRegistry.instance().registerApplication(this);
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            initResourceLoading();
        }
        protected function createRootElement():void{
            m_rootElement = new DocumentView();
            m_rootElement.setApplicationContext(m_appContext);
            addChild(m_rootElement);
            m_rootElement.setParent(m_rootElement);
        }
        protected function loadResources():void{
        }
        protected function initResourceLoading():void{
            m_appContext.coreResourceLoader.addEventListener(Event.COMPLETE, resource_complete);
            loadDefaultResources();
            loadResources();
            if (m_appContext.coreResourceLoader.length()){
                m_appContext.coreResourceLoader.execute();
            } else {
                initApplication();
            };
        }
        protected function lastView_hide():void{
            m_lastView.remove();
            m_lastView = null;
            if (m_currentView){
                m_currentView.show();
            };
        }
        protected function initApplication():void{
            createRootElement();
            m_rootElement.styleSheet = m_css;
            startApplication();
        }
        protected function loadDefaultResources():void{
            if (((Object(this).constructor.hasOwnProperty("CSS_URL")) && (!(Object(this).constructor["CSS_URL"])))){
                return;
            };
            var _local1:String = ((((m_appContext.applicationParameters.css_url) || (Object(this).constructor["CSS_URL"]))) || (CSS_URL));
            m_css = new CSS(_local1);
            m_css.setBaseURL(applicationURL());
            addResource(m_css);
        }
        protected function self_addedToStage(_arg1:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE, self_addedToStage);
            initialize();
        }
        public function rootElement():DocumentView{
            return (m_rootElement);
        }
        public function browserLocation():String{
            if (ExternalInterface.available){
                ExternalInterface.marshallExceptions = true;
                try {
                    return (ExternalInterface.call("function getLocation(){return window.location.href}"));
                } catch(error:Error) {
                    log(error);
                };
            };
            return ("");
        }
        public function applicationURL():String{
            return (loaderInfo.url.split("\\").join("/"));
        }

    }
}//package reprise.core 
